package com.ssm.helong.shop.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ssm.helong.shop.service.SendSms;


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.rmi.ServerException;
import java.util.Map;

public class SendSmsImpl  {
    /**
     * 发送短信
     * @param PhoneNumbers 手机号
     * @param TemplateCode 模板code
     * @param code 参数
     * @return
     */
    public static boolean send(String phoneNumbers, String templateCode) throws UnsupportedEncodingException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GGtLqAmjYdykXjHfX38", "2OmhurDfdGfhVzUv0IdIsHPoTt7Lhi");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", new String("ABC商城".getBytes(),"UTF-8"));
        request.putQueryParameter("TemplateCode", "SMS_205815107");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",templateCode);
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        SendSmsImpl sendSms = new SendSmsImpl();
        SendSmsImpl.send("16638824872","200234");
        System.out.println(System.getProperty("file.encoding"));
        System.out.println(Charset.defaultCharset());
    }
}
